package com.banciyuan.bcywebview.biz.post.write;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.activity.BaseActivity;
import com.banciyuan.bcywebview.base.helper.ActionbarHelper;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;

import de.greenrobot.daoexample.model.PostItem;

/**
 * Created by hy on 2015/12/31.
 */
public class WriteRightsActivity extends BaseActivity implements View.OnClickListener, ActionbarHelper.OnActionCallbacks {
    private ActionbarHelper mActionbarHelper;
    private PostItem postItem;
    private PostItem.Authority authority;
    private View mActionBarView;
    private RelativeLayout rl_everyone;
    private RelativeLayout rl_login;
    private RelativeLayout rl_fans;
    private ImageView iv_everyone;
    private ImageView iv_login;
    private ImageView iv_fans;
    private ImageView iv_repost;
    private ImageView iv_fix;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.post_rights_set);
        initArgs();
        initActionbar();
        initUi();
        getState();
        initAction();
    }

    @Override
    protected void initArgs() {
        postItem = (PostItem) getIntent().getSerializableExtra(gotoUtil.INTENT_VALUE_ONE);
        authority = postItem.getAuthority();
    }

    private void getState() {
        if (authority.getView().equals("all")) {
            setViewAll();
        } else if (authority.getView().equals("login")) {
            setViewLogin();
        } else if (authority.getView().equals("fans")) {
            setViewFans();
        }

        if (authority.getTransmit().isNo_trans()) {
            iv_repost.setImageDrawable(getResources().getDrawable(R.drawable.switch_on));
        } else {
            iv_repost.setImageDrawable(getResources().getDrawable(R.drawable.switch_off));
        }

        if (authority.getTransmit().isNo_modify()) {
            iv_fix.setImageDrawable(getResources().getDrawable(R.drawable.switch_on));
        } else {
            iv_fix.setImageDrawable(getResources().getDrawable(R.drawable.switch_off));
        }
    }

    @Override
    protected void initActionbar() {
        mActionBarView = findViewById(R.id.base_action_bar);
        mActionbarHelper = new ActionbarHelper(this, mActionBarView);
        mActionbarHelper.setTitle(getString(R.string.rights_set));
        mActionbarHelper.setActionCallbacks(this);
    }

    @Override
    protected void initUi() {
        findViewById(R.id.tv_savepic_intro).setVisibility(View.GONE);
        findViewById(R.id.rl_savepic).setVisibility(View.GONE);
        findViewById(R.id.tv_savepic_intro_bottom).setVisibility(View.GONE);
        findViewById(R.id.rl_watermark).setVisibility(View.GONE);
        findViewById(R.id.savepic_line).setVisibility(View.GONE);
        findViewById(R.id.watermark_line).setVisibility(View.GONE);


        rl_everyone = (RelativeLayout) findViewById(R.id.rl_everyone);
        rl_login = (RelativeLayout) findViewById(R.id.rl_login);
        rl_fans = (RelativeLayout) findViewById(R.id.rl_fans);
        iv_everyone = (ImageView) findViewById(R.id.everyone_select_iv);
        iv_login = (ImageView) findViewById(R.id.login_select_iv);
        iv_fans = (ImageView) findViewById(R.id.fans_select_iv);
        iv_repost = (ImageView) findViewById(R.id.repost_select_iv);
        iv_fix = (ImageView) findViewById(R.id.fix_select_iv);
    }

    @Override
    protected void initAction() {
        rl_everyone.setOnClickListener(this);
        rl_login.setOnClickListener(this);
        rl_fans.setOnClickListener(this);
        iv_repost.setOnClickListener(this);
        iv_fix.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rl_everyone:
            case R.id.rl_login:
            case R.id.rl_fans:
                setWorkVisiableRights(v.getId());
                break;
            case R.id.repost_select_iv:
                setRepost();
                break;
            case R.id.fix_select_iv:
                setFix();
                break;
        }
    }

    private void setRepost() {
        if (authority.getTransmit().isNo_trans()) {
            iv_repost.setImageDrawable(getResources().getDrawable(R.drawable.switch_off));
            authority.getTransmit().setNo_trans(false);
        } else {
            iv_repost.setImageDrawable(getResources().getDrawable(R.drawable.switch_on));
            authority.getTransmit().setNo_trans(true);
        }
    }

    private void setFix() {
        if (authority.getTransmit().isNo_modify()) {
            iv_fix.setImageDrawable(getResources().getDrawable(R.drawable.switch_off));
            authority.getTransmit().setNo_modify(false);
        } else {
            iv_fix.setImageDrawable(getResources().getDrawable(R.drawable.switch_on));
            authority.getTransmit().setNo_modify(true);
        }
    }

    private void setWorkVisiableRights(int id) {
        switch (id) {
            case R.id.rl_everyone:
                setViewAll();
                break;
            case R.id.rl_login:
                setViewLogin();
                break;
            case R.id.rl_fans:
                setViewFans();
                break;
        }
    }

    private void setViewAll() {
        iv_everyone.setVisibility(View.VISIBLE);
        iv_login.setVisibility(View.GONE);
        iv_fans.setVisibility(View.GONE);
        authority.setView("all");
    }

    private void setViewLogin() {
        iv_everyone.setVisibility(View.GONE);
        iv_login.setVisibility(View.VISIBLE);
        iv_fans.setVisibility(View.GONE);
        authority.setView("login");
    }

    private void setViewFans() {
        iv_everyone.setVisibility(View.GONE);
        iv_login.setVisibility(View.GONE);
        iv_fans.setVisibility(View.VISIBLE);
        authority.setView("fans");
    }

    private void finishActivity() {
        Intent intent = new Intent();
        intent.putExtra("postitem", postItem);
        setResult(WritePostActivity.GO_SETRIGHTS, intent);
    }

    @Override
    public void onBackPressed() {
        finishActivity();
        super.onBackPressed();
    }

    @Override
    public void onBackClick() {
        finishActivity();
        finish();
    }

    @Override
    public void onActionClick(int id) {

    }
}
